import {page, pageFunc} from "../common/PageBuilder";
import {BasePage, BasePageData} from "../common/core/BasePage";
import {UserPage} from "../common/partPages/UserPage";
import {ScoreRecord} from "../../modules/userModule/data/ScoreRecord";
import {field} from "../../modules/coreModule/data/DataLoader";
import {ItemDetailPage} from "../common/pages/ItemDetailPage";
import {UserInterface} from "../../modules/userModule/UserCommon";
import {handleError} from "../../modules/coreModule/managers/AlertManager";

class Data extends BasePageData {
    @field(Number)
    score: number;
    @field(ScoreRecord)
    item: ScoreRecord;
    @field(String)
    inviteCode: string;
    @field(String)
    inviteCodeConfirmed: string;
    @field(Boolean)
    notifyReceiver: boolean = false;
}

class Param {
    score: number;
}

@page("donation", "积分赠予")
export class DonationPage extends ItemDetailPage<Data, ScoreRecord, Param> {
    /**
     * 部分页
     */
    public userPage: UserPage = new UserPage();

    public data = new Data();

    public onLoad(e) {
        super.onLoad(e);
        this.setData({
            item: new ScoreRecord(),
            score: this.params.score
        }).then();
    }

    @pageFunc
    public onValueChange(e: CustomEvent<any>) {
        this.setData({["item.value"]: e.detail}).then();
    }

    @pageFunc
    public onInviteCodeChange(e: CustomEvent<any>) {
        this.setData({inviteCode: e.detail}).then();
    }

    @pageFunc
    public onInviteCodeConfirmedChange(e: CustomEvent<any>) {
        this.setData({inviteCodeConfirmed: e.detail}).then();
    }

    @pageFunc
    public onDescChange(e: CustomEvent<any>) {
        this.setData({["item.desc"]: e.detail}).then();
    }

    @pageFunc
    public onNotifyReceiverChange(e: CustomEvent<any>) {
        this.setData({notifyReceiver: e.detail}).then();
    }

    @pageFunc
    @handleError
    public doDonate() {
        if (!this.item.value) throw "请输入赠予积分数量";
        if (this.item.value == 0) throw "赠予积分数量不能为0";
        if (this.item.value > this.data.score) throw "积分数量不足";
        if (!this.data.inviteCode) throw "请输入赠予人邀请码";
        if (this.data.inviteCode != this.data.inviteCodeConfirmed) throw "两次邀请码输入不一致";
        UserInterface.DonateScore({inviteCode: this.data.inviteCode, scoreRecord: this.item,
            notifyReceiver: this.data.notifyReceiver})
    }

}
